<?php
error_reporting (E_ALL ^ E_NOTICE);
session_start();
$userid = $_SESSION['userid'];
$username = $_SESSION['username'];

require_once 'interface.php';
webpageDoctype();
print_html_title("Member System - User Preferences");
webpageMetaAndBodyStart();
echo "<p><div align='center'>";
echo "<h2>User Preferences</h2><br/>";
$aChecked = "";
$bChecked = "";
$cChecked = "";
$dChecked = "";
$eChecked = "";
$fChecked = "";
$gChecked = "";
$hChecked = "";
$iChecked = "";
$jChecked = "";
$kChecked = "";
$lChecked = "";
$mChecked = "";
$nChecked = "";
$oChecked = "";

if (true){
	
	if($_POST['submitbtn']){
		$sites = "";
		$prefsites = "";
		$array = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o');
		
		$aSite = $_POST['formSite'];
		if(empty($aSite))
		{
			echo("You didn't select any websites.<br/><a href='userpreference.php'>Return to User Preferences page.</a>");
		}
		else
		{
			$N = count($aSite);
			echo("You selected $N websites(s):<br/>");
			for($i=0; $i < $N; $i++)
			{
				$sites .= $aSite[$i];
				if ($aSite[$i] === "a") {
					echo "CNN News<br/>";
				}
				if ($aSite[$i] === "b") {
					echo "Huffington Post<br/>";
				}
				if ($aSite[$i] === "c") {
					echo "Yahoo News<br/>";
				}
				if ($aSite[$i] === "d") {
					echo "Ars Technica<br/>";
				}
				if ($aSite[$i] === "e") {
					echo "CNET<br/>";
				}
				if ($aSite[$i] === "f") {
					echo "Engadget<br/>";
				}
				if ($aSite[$i] === "g") {
					echo "Gizmodo<br/>";
				}
				if ($aSite[$i] === "h") {
					echo "IGN<br/>";
				}
				if ($aSite[$i] === "i") {
					echo "Joystiq<br/>";
				}
				if ($aSite[$i] === "j") {
					echo "Kotaku<br/>";
				}
				if ($aSite[$i] === "k") {
					echo "Maximum PC<br/>";
				}
				if ($aSite[$i] === "l") {
					echo "Wired<br/>";
				}
				if ($aSite[$i] === "m") {
					echo "CNN Sports Illustrated<br/>";
				}
				if ($aSite[$i] === "n") {
					echo "Sporting News<br/>";
				}
				if ($aSite[$i] === "o") {
					echo "Yahoo Sports<br/>";
				}
			}
			for($i=0; $i < 15; $i++){
				if(strchr($sites, $array[$i])){
					$prefsites .= "1";
				}else{
					$prefsites .= "0";
				}
			}
			require_once './connect.php';
			mysql_query("UPDATE users SET preferences='$prefsites' WHERE username='$username'");
			mysql_close($con);
			echo "<br/><a href='member.php'>Go to Member's page.</a>";
		}
		
	}else{
		require_once './connect.php';
		require_once './rss_parser.php';
		$result = mysql_query("SELECT preferences FROM users WHERE username='$username'");
		$row = mysql_fetch_array($result);
		$prefArray = parse_preferences($row["preferences"]);
		
		if ($prefArray[0] == 1) {
			$aChecked = " CHECKED";
		}
		if ($prefArray[1] == 1) {
			$bChecked = " CHECKED";
		}
		if ($prefArray[2] == 1) {
			$cChecked = " CHECKED";
		}
		if ($prefArray[3] == 1) {
			$dChecked = " CHECKED";
		}
		if ($prefArray[4] == 1) {
			$eChecked = " CHECKED";
		}
		if ($prefArray[5] == 1) {
			$fChecked = " CHECKED";
		}
		if ($prefArray[6] == 1) {
			$gChecked = " CHECKED";
		}
		if ($prefArray[7] == 1) {
			$hChecked = " CHECKED";
		}
		if ($prefArray[8] == 1) {
			$iChecked = " CHECKED";
		}
		if ($prefArray[9] == 1) {
			$jChecked = " CHECKED";
		}
		if ($prefArray[10] == 1) {
			$kChecked = " CHECKED";
		}
		if ($prefArray[11] == 1) {
			$lChecked = " CHECKED";
		}
		if ($prefArray[12] == 1) {
			$mChecked = " CHECKED";
		}
		if ($prefArray[13] == 1) {
			$nChecked = " CHECKED";
		}
		if ($prefArray[14] == 1) {
			$oChecked = " CHECKED";
		}
		echo <<<EOL
			<form action ='./userpreference.php' method='post'>
			Please select your websites<br />
			<table border="0">
			<tr>
				<th colspan="2">NEWS FEEDS</th>
			</tr>
			<tr>
					<td><input type='checkbox' name='formSite[]' value='a'$aChecked /></td><td>CNN News</td>
			</tr>
			<tr>
					<td><input type='checkbox' name='formSite[]' value='b'$bChecked /></td><td>Huffington Post</td>
			</tr>
			<tr>
					<td><input type='checkbox' name='formSite[]' value='c'$cChecked /></td><td>Yahoo News</td>
			</tr>
			<tr>
				<th colspan="2">TECH FEEDS</th>
			</tr>
			<tr>
					<td><input type='checkbox' name='formSite[]' value='d'$dChecked /></td><td>Ars Technica</td>
			</tr>
			<tr>
					<td><input type='checkbox' name='formSite[]' value='e'$eChecked /></td><td>CNET</td>
			</tr>
			<tr>
					<td><input type='checkbox' name='formSite[]' value='f'$fChecked /></td><td>Engadget</td>
			</tr>
			<tr>
					<td><input type='checkbox' name='formSite[]' value='g'$gChecked /></td><td>Gizmodo</td>
			</tr>
			<tr>
					<td><input type='checkbox' name='formSite[]' value='h'$hChecked /></td><td>IGN</td>
			</tr>
			<tr>
					<td><input type='checkbox' name='formSite[]' value='i'$iChecked /></td><td>Joystiq</td>
			</tr>
			<tr>
					<td><input type='checkbox' name='formSite[]' value='j'$jChecked /></td><td>Kotaku</td>
			</tr>
			<tr>
					<td><input type='checkbox' name='formSite[]' value='k'$kChecked /></td><td>Maximum PC</td>
			</tr>
			<tr>
					<td><input type='checkbox' name='formSite[]' value='l'$lChecked /></td><td>Wired</td>
			</tr>
			<tr>
				<th colspan="2">SPORTS FEEDS</th>
			</tr>
			<tr>
					<td><input type='checkbox' name='formSite[]' value='m'$mChecked /></td><td>CNN Sports Illustrated</td>
			</tr>
			<tr>
					<td><input type='checkbox' name='formSite[]' value='n'$nChecked /></td><td>Sporting News</td>
			</tr>
			<tr>
					<td><input type='checkbox' name='formSite[]' value='o'$oChecked /></td><td>Yahoo Sports</td>
			</tr>
			</table>
			<br/>
			<input type='submit' name='submitbtn' value='Submit' />
			</form>
EOL;
		mysql_close($con);
	}
	
}else{
	echo "<p>Please login to access this page. <a href='./login.php'>Login here</a></p>";
}
?>
